package com.aplink.generic.adapter;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;

import com.aplink.generic.GenericFragment;
import com.aplink.generic.pojo.GenericObject;

import java.util.ArrayList;

public class GenericStateViewPagerAdapter extends FragmentStatePagerAdapter {
    private ArrayList<GenericObject> mGenericObjects;
    private String mFullClassName = null;

    public GenericStateViewPagerAdapter( FragmentManager fm,
                                         String fullClassName, ArrayList<GenericObject> genericObjects ) {
        super( fm );
        mGenericObjects = genericObjects;
        mFullClassName = fullClassName;
    }

    @Override
    public Fragment getItem( int arg0 ) {
        try {
            GenericFragment genericFragment = (GenericFragment) Class.forName(
                    mFullClassName ).newInstance();
            GenericObject passedGenericObject = mGenericObjects.get( arg0 );
            genericFragment.setPassedGenericObject( passedGenericObject );
            return genericFragment;
        } catch ( InstantiationException e ) {
            e.printStackTrace();
        } catch ( IllegalAccessException e ) {
            e.printStackTrace();
        } catch ( ClassNotFoundException e ) {
            e.printStackTrace();
        }
        return null;
    }

    @Override
    public int getItemPosition( Object object ) {
        return super.getItemPosition( object );
    }

    @Override
    public int getCount() {
        return mGenericObjects.size();
    }

}
